package org.ethan.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.ethan.basic.exception.BasicException;
import org.ethan.basic.util.AjaxResult;
import org.ethan.basic.util.PageList;
import org.ethan.user.domain.User;
import org.ethan.user.dto.UserDto;
import org.ethan.user.query.UserQuery;
import org.ethan.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户操作
 */
@RestController // @Controller @ResponseBody
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * url: localhost:8080/user/query?id=1  以前的方式
     * url: localhost:8080/user/1  restful的方式
     *
     * @param id
     * @return
     */
    // @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据", notes = "传一个id")
    public User queryById(@PathVariable("id") Long id) {
        return userService.getById(id);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有", notes = "不需要参数")
    public List<User> queryAll() {
        return userService.getAll();
    }

    /**
     * @param query
     * @return {
     * currentPage: 1,
     * pageSize: 10,
     * ....
     * }
     * 查询分页数据
     */
    @PostMapping
    @ApiOperation(value = "分页查询", notes = "分页。。。")
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryData(query);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "传一个id")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            userService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。");
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除", notes = "传id数组")
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            userService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。");
        }
    }

    /**
     * 添加和修改操作
     *
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "用户添加或修改", notes = "如果有id就是修改否则就是添加")
    public AjaxResult addAndUpdate(@RequestBody User user) {
        try {
            if (user.getId() == null) { // 添加操作
                userService.add(user);
            } else { // 修改操作
                userService.update(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }


    /**
     * 手机注册，接收前台参数
     * @param userDto
     * @return
     */
    @PostMapping("/phone/register")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto) {
        try {
            userService.phoneRegister(userDto);
            return AjaxResult.me();
        } catch (BasicException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("System Failure!!!");
        }
    }

}
